package com.googlecode.lookie.swing.list;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.ListCellRenderer;

public abstract class ComboSeparatorsRenderer
    implements ListCellRenderer
{

    private ListCellRenderer delegate;

    private JPanel separatorPanel = new JPanel( new BorderLayout() );

    private JSeparator separator = new JSeparator();

    public ComboSeparatorsRenderer( ListCellRenderer delegate )
    {
        this.delegate = delegate;
    }

    public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected,
                                                   boolean cellHasFocus )
    {
        Component comp = delegate.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus );
        if ( index != -1 && addSeparatorAfter( list, value, index ) )
        { // index==1 if renderer is used to paint current value in combo
            separatorPanel.removeAll();
            separatorPanel.add( comp, BorderLayout.CENTER );
            separatorPanel.add( separator, BorderLayout.SOUTH );
            return separatorPanel;
        }
        else
            return comp;
    }

    protected abstract boolean addSeparatorAfter( JList list, Object value, int index );

}
